package com.gxa.controller;



import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.gxa.common.base.RespResult;
import com.gxa.domain.dto.CablePlanDto;
import com.gxa.domain.entity.CablePlan;
import com.gxa.mapper.CablePlanMapper;
import com.gxa.service.CablePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 吴邓江
 * 大帅哥
 */
@Controller
@RequestMapping("/logistics_cablePlan")
@Slf4j
@Api(tags = "电缆方案管理")
@CrossOrigin(origins = "*")
@SuppressWarnings("all")
public class CablePlanController {
    @Autowired
    private CablePlanService cablePlanService;
    @Resource
    private CablePlanMapper cablePlanMapper;

    @GetMapping("/list")
    @ApiOperation(value = "获取电缆方案列表", notes = "获取电缆方案列表")
    @ResponseBody
    public RespResult list() {
        log.info("获取电缆方案列表");
        List<CablePlan> list = cablePlanService.list();
        return RespResult.success(list);
    }


    /**
     * 新增电缆方案
     *
     * @param cablePlan
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增电缆方案", notes = "新增电缆方案")
    @ResponseBody
    public RespResult save(@RequestBody CablePlan cablePlan) {
        log.info("新增电缆方案信息: {}", cablePlan);
        cablePlanService.save(cablePlan);
        return RespResult.success();
    }

    /**
     * 根据id获取电缆方案详情
     *
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    @ApiOperation(value = "获取电缆方案详情", notes = "获取电缆方案详情")
    @ResponseBody
    public RespResult get(@PathVariable("id") Integer id) {
        log.info("获取电缆方案详情: {}", id);
        CablePlan cablePlan = cablePlanService.getById(id);
        return RespResult.success(cablePlan);
    }

    /**
     * 更新电缆方案
     *
     * @param cablePlan
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新电缆方案", notes = "更新电缆方案")
    @ResponseBody
    public RespResult update(@RequestBody CablePlan cablePlan) {
        log.info("更新电缆方案信息: {}", cablePlan);
        cablePlanService.updateById(cablePlan);
        return RespResult.success();
    }

    /**
     * 删除电缆方案
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}")
    @ApiOperation(value = "删除电缆方案", notes = "删除电缆方案")
    @ResponseBody
    public RespResult delete(@PathVariable("id") Integer id) {
        log.info("删除电缆方案: {}", id);
        cablePlanService.removeById(id);
        return RespResult.success();
    }

    /**
     * 分页查询电缆方案
     * 条件查询
     *
     * @param cablePlanDto
     * @return
     */

    @GetMapping("/page")
    @ApiOperation(value = "分页查询电缆方案", notes = "分页查询电缆方案")
    @ResponseBody
    public RespResult page(CablePlanDto cablePlanDto) {
        log.info("分页查询电缆方案: {}", cablePlanDto);
        return cablePlanService.pagequery(cablePlanDto);
    }

    /**
     * excel表格上传
     * @param file excel表格文件
     */
    @PostMapping("/import")
    @ApiOperation(value = "excel表格上传", notes = "excel表格上传")
    public RespResult imp(@RequestParam("file") MultipartFile file) throws Exception {
        String fileName = "E:\\op\\项目② 物流管理系统\\计划导入模板excel\\"+file.getOriginalFilename();
        log.info("上传文件名: {}", fileName);
        EasyExcel.read(fileName, CablePlan.class, new PageReadListener<CablePlan>(cablePlans1 ->{
           cablePlanService.saveBatch(cablePlans1);
        } )).sheet().doRead();
        return RespResult.success();
    }

    /**
     * 导出电缆方案excel表格
     * @param response
     * @throws Exception
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出电缆方案", notes = "导出电缆方案")
    public void export( HttpServletResponse response) throws Exception {

//        // 设置响应头
//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Content-Disposition", "attachment; filename=users.xlsx");
//
//       Workbook workbook =  cablePlanService.generateExcel();
//        OutputStream outputStream = response.getOutputStream();
//        workbook.write(outputStream);
//        outputStream.close();
//        outputStream.flush();
//
//        workbook.close();

        //        创建HSSFWorkbook对象，excel的文档对象
        HSSFWorkbook workbook = new HSSFWorkbook();
//        excel的表单
        HSSFSheet sheet = workbook.createSheet("电缆方案表");

//        数据库表中的数据
        List<CablePlan> list=cablePlanService.list();

//        设置要导出的文件名
        String fileName="cablePlan"+".xlsx";
//        新增数据行，并且设置单元格数据
        int rowNum=1;
        String[] headers={"项目编号","项目名称","电缆名称",
                "电压等级","电缆截面","抽取时间","入库状态","入库数量",
                "出库状态","出库数量","派单状态","计划状态"};
//        headers 标识excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
//        在excel表中添加表头
        for(int i=0;i<headers.length;i++){
            HSSFCell cell=row.createCell(i);
            HSSFRichTextString text=new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
//        在表头中存放查询到的数据放入对应的列
        for(CablePlan cablePlan:list){
            HSSFRow row1 = sheet.createRow(rowNum++);
            row1.createCell(0).setCellValue(cablePlan.getProjectId());
            row1.createCell(1).setCellValue(cablePlan.getProjectName());
            row1.createCell(2).setCellValue(cablePlan.getCableName());
            row1.createCell(3).setCellValue(cablePlan.getVoltageName());
            row1.createCell(4).setCellValue(cablePlan.getExtractionLength());
            row1.createCell(5).setCellValue(cablePlan.getExtractionDate());
            row1.createCell(6).setCellValue(cablePlan.getReceivingStatus());
            row1.createCell(7).setCellValue(cablePlan.getReceivingNumber());
            row1.createCell(8).setCellValue(cablePlan.getShippingStatus());
            row1.createCell(9).setCellValue(cablePlan.getShippingNumber());
            row1.createCell(10).setCellValue(cablePlan.getDispatchStatus());
            row1.createCell(11).setCellValue(cablePlan.getPlanStatus());
        }
//        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.setHeader("Content-Disposition", "attachment;filename="+fileName);
        response.setHeader("Content-Type", "application/octet-stream");
        response.setContentType("application/octet-stream; charset=UTF-8");
        response.flushBuffer();
        workbook.write(response.getOutputStream());


    }



}
